package com.gemalto.Kryptik.Gui;

import javax.swing.JLabel;
import javax.swing.text.JTextComponent;
import com.gemalto.Kryptik.*;
import com.gemalto.Kryptik.Util.*;

public class Counter extends JLabel {

	public static final boolean DEC = false;
	public static final boolean HEX = true;


	private boolean format;
	private JTextComponent textArea;

	private String text1, text2, flag1="", flag2="";
	private int counter1, counter2;

	public Counter(){
		super();
                setBorder(new javax.swing.border.EtchedBorder());
          	setFont(new java.awt.Font ("Dialog", 0, 11));
		setForeground((java.awt.Color) javax.swing.UIManager.getDefaults ().get ("CheckBoxMenuItem.acceleratorForeground"));
          	//setText("0:0");
		format=Util.getIni().getBoolValue("COUNTER","HEX_FORMAT");
		//format=false;
          	setToolTipText("Length of plain:encrypted text");
          	addMouseListener(new java.awt.event.MouseAdapter() {
              		public void mouseClicked(java.awt.event.MouseEvent evt) {
                  		mouseClickedHandler(evt);
              		}
          	});
		update();
	}


  	private void mouseClickedHandler (java.awt.event.MouseEvent evt) {
		toggleMode();
	    	update();
  	}




	int countSpaces(String t, int start, int stop){
	   int cc=0;
	   for ( int i = start; i < stop; i++ ) if ( t.charAt(i)==' ') cc++;
	   return cc;
	}

	String int2hex(int b){
	    String s = Integer.toHexString(b).toUpperCase();
	    if ( s.length()%2!=0 ){
		s = "0" + s;
	    }
	    return s;
	}


	public JTextComponent getActiveArea(){
		return textArea;
	}

	public void setActiveArea(JTextComponent ta){
		textArea = ta;
	}


	public void toggleMode(){
		setMode(!format);
	}

	public void setMode(boolean format){
		this.format = format;
		Util.getIni().setValue("COUNTER","HEX_FORMAT",format);
		update();
	}


	public boolean getMode(){
		return format;
	}



	public void update(JTextComponent textArea){
		this.textArea = textArea;
		update();
	}




	public void update(){
	    String t;
	    int cp;
	    String separator = ":";

	    boolean isSelection=false;
	    
	    if (textArea!=null) {
	    
		    if ( (t=textArea.getSelectedText())==null ){
			t = textArea.getText();
			cp = textArea.getCaretPosition();
		    } else {
			cp = textArea.getCaretPosition()-textArea.getSelectionStart();
			isSelection=true;
		    }
		    
		    int l = t.length();
		    
		    
		    // Count whitespaces
		    int c = 0, cc = 0; 
		    
		    // ... upto caret position
		    cc=countSpaces(t,0,cp);
		    
		    // ...from caret to the end
		    c=countSpaces(t,cp,l);

		    // If we work with selection....
		    if ( isSelection ){
			//... we dont care what is above selectionEnd and before selectionStart
			c=cc+c;
			cc=0;
			
			// instead of caretPosition show selectionStart
			cp=textArea.getSelectionStart()-countSpaces(textArea.getText(),0,textArea.getSelectionStart());;
		    }
		    
		    counter1=(cp-cc)/2;
		    counter2=(l - cc -c)/2;
	   	    flag1 = ((cp-cc) % 2 == 1)?"+":"";
	    	    flag2 = ((l-cc-c) % 2 == 1)?"+":"";
	    }
	    
	   // String text1 = ((format)?"0x"+int2hex((cp-cc)/2):Integer.toString((cp-cc)/2));
	   // String flag1 = ((cp-cc) % 2 == 1)?"+":"";
	   text1 = ((format)?"0x"+int2hex(counter1):Integer.toString(counter1));
	    
	    //String text2 = ((format)?"0x"+int2hex((l- cc -c)/2):Integer.toString((l- cc -c)/2));
	    //String flag2 = ((l-cc-c) % 2 == 1)?"+":"";
	    text2 = ((format)?"0x"+int2hex(counter2):Integer.toString(counter2));
	    //if (((l-cc-c) % 2)==1) text2.append("+");

	    setText(text1+ flag1+":"+text2+flag2);
	}



}
